<?php
namespace PsiFramework\Applications\AlloMediaPortal\Classes\Actions;

import("Classes.Actions.AbstractAction");
import("Classes.Util.Object.Identification");

use \PsiFramework\Classes\Actions\AbstractAction;
use \PsiFramework\Classes\Factory\DAOFactory;
use \PsiFramework\Classes\Util\Object\Date;
use \PsiFramework\Applications\AlloMediaPortal\Classes\Model\AlloMediaTrace;
use \PsiFramework\Classes\Util\Manager\ApplicationManager;

/**
 * PsiFramework
 * Php Simple Framework
 *
 * @author Rémi San
 * @version beta
 */

/**
 * The AlloMediaTrace Action
 *
 * This action is used to trace the user actions
 *
 * @package Actions.Util
 */
class AlloMediaTraceAction extends AbstractAction
{

    /**
     * Function executed by the controller
     *
     * It traces the user actions
     *
     * @return Response    the response to the action execution
     */
    public function execute(/*Identification*/ $identification=null)
    {
        parent::execute();
        
        $url = $this->_request->getParameter('url');
        $actionName = $this->_request->getParameter('actionName');
        $application = ApplicationManager::getInstance()->getApplication()->getName();
        
        $formId = $this->_request->getParameter('form_id');
        $form = null;
        if (!isNullOrEmptyString($formId)) {
            $formDao = DAOFactory::getDAO('Form');
            $form = $formDao->retrieveByPk($formId, false, false);
        }
        
        $entryId = $this->_request->getParameter('entry_id');
        $entry = null;
        if (!isNullOrEmptyString($entryId)) {
            $entryDao = DAOFactory::getDAO('AlloMediaEntry');
            $entry = $entryDao->retrieveByPk($entryId, false, false);
        }
        
        $traceDao = DAOFactory::getDAO('Trace');
        $trace = new AlloMediaTrace();
        $trace->setUser($this->_user);
        $trace->setUrl($url);
        $trace->setAction($actionName);
        $trace->setApplication($application);
        $trace->setTime(Date::now());
        $trace->setEntry($entry);
        $trace->setTheme($form);
        
        $traceDao->save($trace, true, false);
        
        $this->findForward("success");
    }
}
